﻿using System;
using System.Collections.Generic;
using System.Text;
using RockCompany.Common.Lib;
using RockCompany.Master.DTO;
using RockCompany.Master.Constant;
using RockCompany.Common.Constant;
using System.Windows.Forms;
using System.Data;

namespace RockCompany.Master.Model
{
    public class SI05BFormModel : CommonModel
    {
        #region Properties
        private DataTable _prodExportList;
        public DataTable prodExportList
        {
            get { return _prodExportList; }
            set { _prodExportList = value; }
        }

        private DataTable _prodExportDetail;
        public DataTable prodExportDetail 
        {
            get { return _prodExportDetail; }
            set { _prodExportDetail = value; } 
        }
        #endregion

        #region Constructor
        public SI05BFormModel()
        {
            dbConnector = new DBConnector();
            prodExportList = new DataTable();
            prodExportDetail = new DataTable();
            DataList = new List<object>();
        } 
        #endregion

        #region Utility Methods
        public DataRow getCheckedRow(int index)
        {
            DataRow curRow = prodExportList.Rows[index];
            curRow.SetField<bool>(PHIEU_XH_DTO.N_CHON, !curRow.Field<bool>(PHIEU_XH_DTO.N_CHON));
            return curRow;
        }
        #endregion

        #region Main Methods
        public DataTable searchProdExportList(string sNo, string sfromDate, string sToDate)
        {
            string sSql = "SELECT CONVERT(BIT,'0') AS CHON,*";
            sSql += " FROM " + PHIEU_XH_DTO.N_TABLE + "";
            sSql += " WHERE " + PHIEU_XH_DTO.N_MS_PHIEU_XH + " LIKE '" + sNo + "%'";
            sSql += " AND CONVERT(VARCHAR," + PHIEU_XH_DTO.N_CREATION_TIME + ",112) BETWEEN " + sfromDate + " AND " + sToDate;
            prodExportList = selectData(sSql).Copy();
            return prodExportList;
        }

        public DataTable searchProdExportDetail(string sProdExportNo)
        {
            string sSql = "SELECT CONVERT(BIT,'0') AS CHON,*";
            sSql += " FROM " + PHIEU_XH_CT_DTO.N_TABLE;
            sSql += " WHERE " + PHIEU_XH_CT_DTO.N_MS_PHIEU_XH + " = '" + sProdExportNo + "'";
            prodExportDetail = selectData(sSql).Copy();
            return prodExportDetail;
        }

        public DataTable getSelectedProd()
        {
            DataTable res = prodExportDetail.Clone();
            foreach (DataRow curRow in DataList)
            {
                DataTable temp = searchProdExportDetail(curRow[PHIEU_XH_DTO.N_MS_PHIEU_XH].ToString());
                foreach (DataRow detailRow in temp.Rows)
                {
                    res.ImportRow(detailRow);
                }
            }
            return res;
        }

        public bool removeProdExport(string sProdExport)
        {
            string sClause = " WHERE " + PHIEU_XH_DTO.N_MS_PHIEU_XH + " = '" + sProdExport + "'";
            return removeRow(PHIEU_XH_DTO.N_TABLE, sClause, null);
        }

        public bool removeProdExportDetail(string sProdExport)
        {
            string sClause = " WHERE " + PHIEU_XH_CT_DTO.N_MS_PHIEU_XH + " = '" + sProdExport + "'";
            return removeRow(PHIEU_XH_CT_DTO.N_TABLE, sClause, null);
        }

        public bool export2Excel(string newPath, PHIEU_XH_DTO prodExport)
        {
            bool b = false;
            ExcelExporter excelExport = new ExcelExporter();
            excelExport.Trans = this.translator;
            if (excelExport.open(SI05BConst.EX_NAME))
            {
                excelExport.inputValue(SI05BConst.EX_POS_KHACH_HANG, prodExport.KHACH_HANG);
                excelExport.inputValue(SI05BConst.EX_POS_DIA_CHI, prodExport.DIA_CHI_KH);
                excelExport.inputValue(SI05BConst.EX_POS_HOA_DON, prodExport.KEM_HOA_DON);
                excelExport.inputValue(SI05BConst.EX_POS_NGAY_LAP_PHIEU, prodExport.CREATION_TIME.ToShortDateString());

                List<DataColumn> disCol = new List<DataColumn>();
                disCol.Add(prodExportDetail.Columns[PHIEU_XH_CT_DTO.N_MA_MAU]);
                disCol.Add(prodExportDetail.Columns[PHIEU_XH_CT_DTO.N_SL_XUAT]);
                disCol.Add(prodExportDetail.Columns[PHIEU_XH_CT_DTO.N_TRONG_LUONG]);
                disCol.Add(prodExportDetail.Columns[PHIEU_XH_CT_DTO.N_SO_LO]);
                disCol.Add(prodExportDetail.Columns[PHIEU_XH_CT_DTO.N_GHI_CHU]);

                excelExport.inputRangeValues(prodExportDetail, disCol, SI05BConst.EX_BEGIN_GRID_COL, SI05BConst.EX_BEGIN_GRID_ROW, true);
                excelExport.inputValue(SI05BConst.EX_POS_PHE_DUYET + Convert.ToString(prodExportDetail.Rows.Count + SI05BConst.EX_BEGIN_GRID_ROW + 1), SI05BConst.EX_VAL_PHE_DUYET);
                excelExport.inputValue(SI05BConst.EX_POS_PHONG_KD + Convert.ToString(prodExportDetail.Rows.Count + SI05BConst.EX_BEGIN_GRID_ROW + 1), SI05BConst.EX_VAL_PHONG_KD);
                excelExport.inputValue(SI05BConst.EX_POS_THU_KHO + Convert.ToString(prodExportDetail.Rows.Count + SI05BConst.EX_BEGIN_GRID_ROW + 1), SI05BConst.EX_VAL_THU_KHO);
                excelExport.inputValue(SI05BConst.EX_POS_TAI_XE + Convert.ToString(prodExportDetail.Rows.Count + SI05BConst.EX_BEGIN_GRID_ROW + 7), SI05BConst.EX_VAL_TAI_XE);
                excelExport.inputValue(SI05BConst.EX_POS_KH + Convert.ToString(prodExportDetail.Rows.Count + SI05BConst.EX_BEGIN_GRID_ROW + 7), SI05BConst.EX_VAL_KH);
                b = excelExport.saveExcel(newPath);
                excelExport.close();
            }
            return b;
        }
        #endregion
    }
}
